package com.sict.myapplication;



import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
private int keypoardview_height;
private int keypoardview_width;
private List<KeyboardParameter> KeyboardParameterlist=new ArrayList();
private Map<Integer,LinearLayout> KeyboardLineDic=new HashMap();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout keypoardview = findViewById(R.id.keypoardview);
        keypoardview.post(new Runnable() {
            @Override
            public void run() {
                keypoardview_height=keypoardview.getHeight();
                keypoardview_width=keypoardview.getWidth();
                initKeyboardLineDic();
                initKeyboardParameterlist();
                makeKeyBoard();
            }
        });
    }
    private void initKeyboardLineDic(){
        KeyboardLineDic.put(1,findViewById(R.id.keyboardline1));
        KeyboardLineDic.put(2,findViewById(R.id.keyboardline2));
        KeyboardLineDic.put(3,findViewById(R.id.keyboardline3));
        KeyboardLineDic.put(4,findViewById(R.id.keyboardline4));
        KeyboardLineDic.put(5,findViewById(R.id.keyboardline5));
        KeyboardLineDic.put(6,findViewById(R.id.keyboardline6));

    }

    private void makeKeyBoard(){
        for(int i=0;i<KeyboardParameterlist.size();i++){
            KeyboardParameter keyboardParameter = KeyboardParameterlist.get(i);
            makeKey(keyboardParameter);
        }
        KeyboardLineDic.get(6).addView(creatDirectionKey());
    }
    private void makeKey(KeyboardParameter keyboardParameter){
        TextView keyview=new TextView(MainActivity.this);
        KeyboardLineDic.get(keyboardParameter.getKeyboardRow()).addView(keyview);
        LinearLayout.LayoutParams layoutParams = ( LinearLayout.LayoutParams)keyview.getLayoutParams();
        layoutParams.width=(int)(keyboardParameter.getKeyboardWidth()*keyboardParameter.getKeyboardWidthscale());
        layoutParams.height=keyboardParameter.getKeyboardHeight();
        layoutParams.setMargins(2,2,2,2);
        layoutParams.gravity= Gravity.CENTER;
        keyview.setGravity(Gravity.CENTER);
        keyview.setLayoutParams(layoutParams);
        keyview.setText(keyboardParameter.getKeyboardName());
        if(keyboardParameter.getKeyboardName().length()<5){
            keyview.setTextSize(11);
        }else {
            keyview.setTextSize(8);
        }

        keyview.setTextColor(Color.parseColor("#000000"));
        keyview.setBackgroundColor(Color.parseColor("#cecece"));
    }
    private View   creatDirectionKey(){
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.directionkeyboard, null);
        layout.setLayoutParams(new LinearLayout.LayoutParams((keypoardview_width/15-4)*3,keypoardview_height/6));
        return layout;
    }
    private void initKeyboardParameterlist(){
        int keypoard_height=keypoardview_width/14-4;
        int line1_width=keypoardview_width/14-4;
        int line2_width=keypoardview_width/15-4;
        int line3_width=keypoardview_width/15-4;
        int line4_width=keypoardview_width/15-4;
        int line5_width=keypoardview_width/15-4;
        int line6_width=keypoardview_width/15-4;
        KeyboardParameterlist.add(new KeyboardParameter("ESC",1,keypoard_height,line1_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("F1",1,keypoard_height,line1_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("F2",1,keypoard_height,line1_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("F3",1,keypoard_height,line1_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("F4",1,keypoard_height,line1_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("F5",1,keypoard_height,line1_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("F6",1,keypoard_height,line1_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("F7",1,keypoard_height,line1_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("F8",1,keypoard_height,line1_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("F9",1,keypoard_height,line1_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("F10",1,keypoard_height,line1_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("F11",1,keypoard_height,line1_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("F12",1,keypoard_height,line1_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("DELETE",1,keypoard_height,line1_width,1));

        KeyboardParameterlist.add(new KeyboardParameter("~",2,keypoard_height,line2_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("1",2,keypoard_height,line2_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("2",2,keypoard_height,line2_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("3",2,keypoard_height,line2_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("4",2,keypoard_height,line2_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("5",2,keypoard_height,line2_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("6",2,keypoard_height,line2_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("7",2,keypoard_height,line2_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("8",2,keypoard_height,line2_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("9",2,keypoard_height,line2_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("0",2,keypoard_height,line2_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("-",2,keypoard_height,line2_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("=",2,keypoard_height,line2_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("back",2,keypoard_height,line2_width,2));

        KeyboardParameterlist.add(new KeyboardParameter("Tab",3,keypoard_height,line3_width,2));
        KeyboardParameterlist.add(new KeyboardParameter("q",3,keypoard_height,line3_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("w",3,keypoard_height,line3_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("e",3,keypoard_height,line3_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("r",3,keypoard_height,line3_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("t",3,keypoard_height,line3_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("y",3,keypoard_height,line3_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("u",3,keypoard_height,line3_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("i",3,keypoard_height,line3_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("o",3,keypoard_height,line3_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("p",3,keypoard_height,line3_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("[",3,keypoard_height,line3_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("]",3,keypoard_height,line3_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("\\",3,keypoard_height,line3_width,1));

        KeyboardParameterlist.add(new KeyboardParameter("caps lock",4,keypoard_height,line4_width,2));
        KeyboardParameterlist.add(new KeyboardParameter("a",4,keypoard_height,line4_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("s",4,keypoard_height,line4_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("d",4,keypoard_height,line4_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("f",4,keypoard_height,line4_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("g",4,keypoard_height,line4_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("h",4,keypoard_height,line4_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("j",4,keypoard_height,line4_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("k",4,keypoard_height,line4_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("l",4,keypoard_height,line4_width,1));
        KeyboardParameterlist.add(new KeyboardParameter(":",4,keypoard_height,line4_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("\"",4,keypoard_height,line4_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("回车",4,keypoard_height,line4_width,2));

        KeyboardParameterlist.add(new KeyboardParameter("shift",5,keypoard_height,line5_width,2.5f));
        KeyboardParameterlist.add(new KeyboardParameter("z",5,keypoard_height,line5_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("x",5,keypoard_height,line5_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("c",5,keypoard_height,line5_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("v",5,keypoard_height,line5_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("b",5,keypoard_height,line5_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("n",5,keypoard_height,line5_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("m",5,keypoard_height,line5_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("<",5,keypoard_height,line5_width,1));
        KeyboardParameterlist.add(new KeyboardParameter(">",5,keypoard_height,line5_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("?",5,keypoard_height,line5_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("shift",5,keypoard_height,line5_width,2.5f));

        KeyboardParameterlist.add(new KeyboardParameter("ctrl",6,keypoard_height,line6_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("fn",6,keypoard_height,line6_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("win",6,keypoard_height,line6_width,1));
        KeyboardParameterlist.add(new KeyboardParameter("ctrl",6,keypoard_height,line6_width,1.2f));
        KeyboardParameterlist.add(new KeyboardParameter("空格",6,keypoard_height,line6_width,5));
        KeyboardParameterlist.add(new KeyboardParameter("alt",6,keypoard_height,line6_width,1.2f));
        KeyboardParameterlist.add(new KeyboardParameter("ctrl",6,keypoard_height,line6_width,1));






   }
}